package com.lkm.app_ui.m.Imple.steelyard_ser;

import android.os.Looper;

import com.lkm.app_ui.MyApplication;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.m.steelyard_com.OnSteelyardStateEvent;
import com.lkm.comlib.Config;
import com.lkm.frame.task2.InterceptorAdapter;
import com.lkm.frame.task2.Task;
import com.lkm.hardware_connect.AbsOnDeviceStateEvent;
import com.lkm.hardware_connect.IHardwareControler;
import com.lkm.javatool.RandomStringUtils;
import com.lkm.market2_0.m_imple.steelyardserver.SteelyardServerModuleBase;
import com.lkm.serialport_com.connect.SerialportConnectModule;
import com.lkm.serialport_common_com.connect.SerialportConfig;

public class SerialportCom extends SerialportConnectModule {


    private SteelyardServerModuleBase mSteelyardServerModuleBase;

    public SerialportCom(SteelyardServerModuleBase mSteelyardServerModuleBase) {
        super(MyApplication.getInstance().getTaskManager2(), Looper.getMainLooper(), Config.getTaskExecutor());
        this.mSteelyardServerModuleBase = mSteelyardServerModuleBase;
    }

    @Override
    protected SerialportConfig configSerialportConfig() {
        SerialportConfig serialportConfig = new SerialportConfig();
        serialportConfig.setDevName("/dev/ttyS"+ RunTimeData.serialport);






        serialportConfig.setBaud(115200);

        return serialportConfig;
    }

    @Override
    protected AbsOnDeviceStateEvent createOnDeviceStateEvent(int state, int reConnectCount, boolean isError, String errorLog) {
        return new OnSteelyardStateEvent(state,reConnectCount,isError,errorLog);
    }



    @Override
    protected Task<IHardwareControler, ?, ?> createReadTask(IHardwareControler hardwareControler) {
        Task<IHardwareControler, ?, ?> task = SteelyardServerModuleBase.createReadTask("/dev/ttyS4" + RandomStringUtils.random(6, RandomStringUtils.azAZ90), hardwareControler,mSteelyardServerModuleBase.mSteelyardStatusManagerModule,false);
        task.addInterceptor(new InterceptorAdapter(){

            @Override
            public boolean interceptCancel(Task task) {
                interceptEnd();
                return super.interceptCancel(task);
            }

            @Override
            public boolean interceptComplete(Task task, Object result) {
                interceptEnd();
                return super.interceptComplete(task, result);
            }

            public void interceptEnd() {
                disConnectForError("结束接待");
            }
        });

        return task;


    }
}
